package com.lynn.common.utils.spf

import android.app.Application
import com.lynn.common.utils.file.FileUtil
import java.io.File
import java.util.Locale

/**
 * Desc:   本地存储帮助类
 * Creator ling
 * Date:   2017/5/9 0009 18:27
 */

object LocalCacheUtil {

    private const val FILE_NAME = "app_params"
    private const val ENCRYPT_FILE_NAME = "encrypt_app_params"
    private lateinit var application: Application


    /**
     * Application中初始化
     */
    fun init(app: Application) {
        application = app
    }

    /**
     * 删除spf 文件，App卸载会自动删除
     * public final static String DATA_URL = "/data/data/";
     * File file = new File(DATA_URL + getPackageName().toString() + "/shared_prefs",
     * SHARED_MAIN_XML);
     */
    fun deleteSpfFile() {
        val absolutePath = context.filesDir?.absolutePath + "/shared_prefs"
        val file = File(absolutePath, FILE_NAME)
        FileUtil.deleteFile(file)
    }


    private val context by lazy {
        application.applicationContext
    }

    ///////////////////////////////////////////////////////////////////////////
// 临时变量存储
///////////////////////////////////////////////////////////////////////////
    var language: String
        get() = get("language", Locale.getDefault().language) as String
        set(language) = put("language", language)

//    var isReloadData: Boolean
//        get() = get(
//            RELOAD_FRAGMENT_DATA,
//            true
//        ) as Boolean
//        set(reload) = put(
//            RELOAD_FRAGMENT_DATA,
//            reload
//        )

    var autoLogin: Boolean
        get() = get("auto_login", false) as Boolean
        set(isAutoLogin) = put("auto_login", isAutoLogin)

    var firstStart: Boolean
        get() = get("first_start", true) as Boolean
        set(isFirstUse) = put("first_start", isFirstUse)

///////////////////////////////////////////////////////////////////////////
//  敏感信息存储 TOKEN
///////////////////////////////////////////////////////////////////////////

    var token : String
        get() = getEncrpt("token", "")
        set(token) = putEncrpt("token", token)


///////////////////////////////////////////////////////////////////////////
// 私有方法
///////////////////////////////////////////////////////////////////////////

    private fun get(key: String, defaultValue: Any): Any? {
        return SpfUtil.get(context, FILE_NAME, key, defaultValue)
    }

    private fun put(key: String, value: Any) {
        SpfUtil.put(context, FILE_NAME, key, value)
    }

    private fun remove(key: String) {
        SpfUtil.remove(context, FILE_NAME, key)
    }

    private fun <T> getEncrpt(key: String, defaultValue: T): T {
        return SpfEncryptUtils.Companion.getInstance(context, ENCRYPT_FILE_NAME)
            .get(key, defaultValue)
    }

    private fun <T> putEncrpt(key: String, value: T) {
        SpfEncryptUtils.Companion.getInstance(context, ENCRYPT_FILE_NAME)
            .put(key, value)
    }

    private fun removeEncrpt(key: String) {
        SpfEncryptUtils.Companion.getInstance(context, ENCRYPT_FILE_NAME)
            .remove(key)
    }

}